package com.project.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.project.bean.UserBean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("excel")
public class ExcelController {

    @RequestMapping("add")
    public List<UserBean> add(@RequestParam("excelFile") MultipartFile mf) throws  Exception{
        //定义导入设置
        ImportParams ims = new ImportParams();
        //设置标题栏占几行
        ims.setTitleRows(1);
        //设置表头占几行
        ims.setHeadRows(1);
        //设置读取第几个选项卡内容
        ims.setSheetNum(1);

        //将上传excel文件流，转化为集合。第一个参数为上传文件读取流。第二个参数为集合存放元素类模版
        //第三个参数为导入设置对象
        List<UserBean> list = ExcelImportUtil.importExcel(mf.getInputStream()
                ,UserBean.class,ims);
        return list;
    }
}
